var express = require('express');
var router = express.Router();
var CICI = require('./cici');
var Email = require('./email');
const https = require('http');
var MongoDB = require('../../database/mongodb');

/**
 * 邮件提醒.
 */
router.post('/sendEmail', function(req, res, next) {
    let paramObj = req.body
    let post_options = {
      host: '10.52.0.14',
      port: 5000,
      path: `/works/views/${paramObj.html}`,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json;charset=UTF-8'
      }
    };

    let post_req = https.request(post_options, (res1) => {
        res1.setEncoding('utf-8');
        let chunks = '';
        res1.on('data', (chunk) => {
            chunks += chunk;
        });
        res1.on('end', () => {
            console.log(chunks)
            paramObj.content = chunks
            Email.sendMail(paramObj, (error, res2) => {
                if (error) {
                    res.json({status: 0, mess: error.message});
                } else {
                    res.json({status: 1, data: res2});
                }
            });
        });
    }).on("error", function (err) {
        console.log("error:" + err)
    });
    post_req.write(JSON.stringify(paramObj.data), 'utf8');
    post_req.end();
});

/**
 * 给个人发送CICI消息.
 */
router.post('/sendOldCCMessage', function (req, res, next) {
    let paramObj = req.body;
    let result = [];
    CICI.remindOldCCMess(paramObj.toEmail, paramObj.content, (data) => {
        data.email = paramObj.toEmail;
        result.push(data);
        res.json(result);
    });
});

/**
 * 给个人发送CICI消息.
 */
router.post('/sendMessage', function (req, res, next) {
    let paramObj = req.body;
    let result = [];
    CICI.remindMess(paramObj.toEmail, paramObj.content, paramObj.linkUrl, (data) => {
        if (data.data.code === 0) {
            res.json({
                code: 0,
                message: '发送成功'
            })
        } else {
            res.json({
                code: 1,
                message: '发送失败'
            })
        }
    });
});

/**
 * 给指定范围人员发送CICI消息.
 */
router.post('/sendMessages', function (req, res, next) {
    let emails = req.body.emails;
    let content = req.body.content;   
    let linkUrl = req.body.linkUrl;
    for (let i = 0; i < emails.length; i++) {
        CICI.remindMess(emails[i], content, linkUrl, (res) => {
            console.log(res.data)
        });
    }
    res.json(1);
});

/**
 * 给指定范围人员发送CICI消息.
 */
 router.post('/buildNotify', function (req, res, next) {
    let conditions = {
        type: 0
    }
    MongoDB.find('subscription', conditions, null, null, (err, docs) => {
        if (err) {
            next(new Error('获取发送用户列表失败'))
        } else {
            let emails = docs[0].userId
            let content = req.body.content;
            let linkUrl = req.body.linkUrl;
            for (let i = 0; i < emails.length; i++) {
                CICI.remindMess(emails[i], content, linkUrl, (res) => {
                    console.log(res.data)
                });
            }
            res.json(1);
        }
    });
});

/**
 * 发送页面.
 */
router.post('/remindUrl', function (req, res, next) {
    let paramObj = req.body;
    let result = [];
    CICI.remindUrl(paramObj.toEmail, paramObj.url, (data) => {
        data.email = paramObj.toEmail;
        result.push(data);
    })
    res.json(result);
});

module.exports = router;